<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理页原型</title>
    <!-- 引入组件库 -->
    <script src="../../js/vue.js"></script>
    <link rel="stylesheet" href="../../element-ui/lib/theme-chalk/index.css">
    <script src="../../element-ui/lib/index.js"></script>
    <script src="../../js/axios.js"></script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="user" class="demo-form-inline">
        <el-form-item label="姓名">
            <el-input v-model="user.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
            <el-select v-model="user.sex" placeholder="性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="手机号">
            <el-input v-model="user.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <!--按钮 -->
    <el-row>
        <el-button type="danger" @click="deleteByIds" plain>批量删除</el-button>
        <el-button type="primary" @click="dialogVisible = true" plain>新增</el-button>
    </el-row>

    <!--添加数据对话框-->
    <el-dialog
            title="编辑用户信息"
            :visible.sync="dialogVisible"
            width="30%">
        <!--表单-->
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="姓名">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="性别" width="100%">
                <el-select :model="form.sex" placeholder="请选择">
                    <el-option value="男"></el-option>
                    <el-option value="女"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="联系方式">
                <el-input type="number" v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="工作单位">
                <el-input v-model="form.workplace"></el-input>
            </el-form-item>
            <el-form-item label="身份证号">
                <el-input v-model="form.idCard"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" v-if="update==0" @click="addUser">提交</el-button>
                <el-button type="primary" v-else @click="updateUser">确认修改</el-button>
                <el-button @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--表格-->
    <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :row-class-name="tableRowClassName"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55"
                fixed
        >
        </el-table-column>
        <el-table-column
                type="index"
                width="50"
                align="center"
                fixed
        >
        </el-table-column>
        <el-table-column
                prop="name"
                label="姓名"
                align="center"
        >
        </el-table-column>
        <el-table-column
                prop="sex"
                label="性别"
                align="center"
        >
        </el-table-column>
        <el-table-column
                prop="phone"
                label="联系方式"
                align="center"
        >
        </el-table-column>
        <el-table-column
                prop="workplace"
                label="工作地点"
                align="center"
        >
        </el-table-column>
        <el-table-column
                prop="idCard"
                label="身份证号"
                align="center"
        >
        </el-table-column>
        <el-table-column
                prop="method"
                label="操作"
                align="center"
        >
            <template slot-scope="scope">
                <el-row>
                    <el-button type="primary" @click="handleUpdate(scope.$index, scope.row)">修改</el-button>
                    <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </el-row>
            </template>
        </el-table-column>
    </el-table>
    <!--分页工具条    -->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>
</div>
<script>
    new Vue({
        el: "#app",
        mounted() {
            this.selectAll()
        },
        methods: {
            cancel() {
                this.dialogVisible = false
                this.update = 0
                this.form = {
                    name: '',
                    sex: '',
                    phone: "",
                    workplace: '',
                    idCard: ""
                }
            },
            // selectAll(){
            //     axios({
            //         method: "get",
            //         url: "http://localhost/user"
            //     }).then((resp) => {
            //         console.log(resp)
            //         this.tableData = resp.data;
            //     })
            // },

            //查询分页数据
            selectAll() {
                this.loading=true
                axios({
                    method: "get",
                    url: "http://localhost/user/" + this.currentPage + "/" + this.pageSize
                }).then((resp) => {
                    //设置表格数据
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;

                    this.loading=false
                })
            },
            //分页条件查询
            selectByCondition() {
                this.loading=true

                // console.log(this.user)
                if (this.user.phone == '') {
                    this.user.phone = null
                }
                axios({
                    method: "post",
                    url: "http://localhost/user/" + this.currentPage + "/" + this.pageSize,
                    data: this.user
                }).then((resp) => {
                    console.log(resp)
                    //设置表格数据
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;
                    this.loading=false
                })
            },
            //每行类名
            tableRowClassName({row, rowIndex}) {
                if (rowIndex % 4 === 1) {
                    return 'warning-row';
                } else if (rowIndex % 4 === 3) {
                    return 'success-row';
                }
                return '';
            },

            //复选
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(val)
            },

            //搜索
            onSubmit() {
                this.currentPage = 1
                // console.log(this.user);
                this.selectByCondition()
            },
            //修改
            handleUpdate(index, row) {
                console.log(index, row);
                this.form = row
                this.update = 1
                this.dialogVisible = true

            },
            updateUser() {
                console.log(this.form)
                axios({
                    method: "put",
                    url: "http://localhost/user",
                    data: this.form
                }).then((resp) => {
                    if (resp.data == "success") {
                        this.update = 0
                        this.dialogVisible = false
                        this.selectAll()
                        this.$message({
                            message: "修改成功",
                            type: "success"
                        })
                    }
                })
                this.cancel()
            },
            //添加数据
            addUser() {
                console.log(this.form)
                axios({
                    method: "post",
                    url: "http://localhost/user",
                    data: this.form
                }).then((resp) => {
                    if (resp.data == "success") {
                        this.dialogVisible = false
                        this.selectAll()
                        this.$message({
                            message: "添加成功",
                            type: "success"
                        })
                    }
                })
                this.cancel()
            },

            //删除单个
            handleDelete(index, row) {
                console.log(index, row);
                //弹出操作对话框
                this.$confirm('此操作将删除选择的数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        method: "delete",
                        url: "http://localhost/user/" + row.id,
                    }).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll()
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },

            //批量删除
            deleteByIds() {
                //弹出操作对话框
                this.$confirm('此操作将删除选择的数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // console.log(this.multipleSelection)
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        this.selectedIds[i] = this.multipleSelection[i].id;
                    }
                    // console.log(this.selectedIds)
                    axios({
                        method: "delete",
                        url: "http://localhost/user",
                        data: this.selectedIds
                    }).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll()
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },

            //分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.selectByCondition()
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage = val
                this.selectByCondition()
            }
        },
        data() {
            return {
                //加载
                loading:true,
                //搜索
                user: {
                    name: null,
                    sex: null,
                    phone: null,
                },
                //对话框是否可见
                dialogVisible: false,
                update: 0,
                form: {
                    name: '',
                    sex: '',
                    phone: "",
                    workplace: '',
                    idCard: ""
                },
                selectedIds: [],
                //复选框选中数据集合
                multipleSelection: [],
                //表格数据
                tableData: [],
                totalCount: '',
                currentPage: 1,
                //每页显示条数
                pageSize: 5
            }
        }
    })
</script>
</body>
</html>